﻿using System;
using System.IO;
using System.Windows.Forms;

namespace FileMergeApp
{
    public partial class MainForm : Form
    {
        private string file1Path;
        private string file2Path;
        public MainForm()
        {
            InitializeComponent();
        }
        private void btnSelectFile1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    file1Path = openFileDialog.FileName;
                    txtFile1Path.Text = file1Path;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("选择文件时出现错误：" + ex.Message);
                }
            }
        }
        private void btnSelectFile2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    file2Path = openFileDialog.FileName;
                    txtFile2Path.Text = file2Path;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("选择文件时出现错误：" + ex.Message);
                }
            }
        }
        private void btnMerge_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(file1Path) || string.IsNullOrEmpty(file2Path))
            {
                MessageBox.Show("请选择两个文件");
                return;
            }

            string dataDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
            if (!Directory.Exists(dataDirectory))
            {
                try
                {
                    Directory.CreateDirectory(dataDirectory);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("创建数据目录时出现错误：" + ex.Message);
                    return;
                }
            }

            string mergedFilePath = Path.Combine(dataDirectory, "merged.txt");

            try
            {
                using (StreamWriter writer = new StreamWriter(mergedFilePath))
                {
                    string file1Content = File.ReadAllText(file1Path);
                    writer.WriteLine(file1Content);

                    string file2Content = File.ReadAllText(file2Path);
                    writer.WriteLine(file2Content);
                }

                MessageBox.Show("合并完成");
            }
            catch (Exception ex)
            {
                MessageBox.Show("写入文件时出现错误：" + ex.Message);
            }
        }       
    }
}